<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>高效CI/CD流水线脚本指南 | 技术小馆</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            line-height: 1.8;
            color: #333;
        }
        h1, h2, h3, h4, h5, h6 {
            font-family: 'Noto Serif SC', serif;
            font-weight: 600;
        }
        .hero-gradient {
            background: linear-gradient(135deg, #6e8efb 0%, #a777e3 100%);
        }
        .code-block {
            background: #2d3748;
            color: #f8fafc;
            border-radius: 0.5rem;
            overflow: hidden;
        }
        .code-header {
            background: #1a202c;
            padding: 0.5rem 1rem;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        .code-content {
            padding: 1rem;
            overflow-x: auto;
        }
        .tool-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
        }
        .stage-item::before {
            content: "";
            position: absolute;
            left: 0;
            top: 0;
            height: 100%;
            width: 4px;
            background: linear-gradient(to bottom, #6e8efb, #a777e3);
        }
        .hover-grow {
            transition: all 0.3s ease;
        }
        .hover-grow:hover {
            transform: scale(1.03);
        }
    </style>
</head>
<body class="bg-gray-50">
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20 px-4">
        <div class="container mx-auto max-w-5xl">
            <div class="flex flex-col items-center text-center">
                <div class="mb-6">
                    <span class="px-4 py-2 bg-white bg-opacity-20 rounded-full text-sm font-medium">CI/CD最佳实践</span>
                </div>
                <h1 class="text-4xl md:text-5xl font-bold mb-6">高效CI/CD流水线脚本指南</h1>
                <p class="text-xl md:text-2xl max-w-3xl opacity-90 mb-8">
                    自动化构建、测试和部署的艺术 - 提升开发效率，确保代码质量
                </p>
                <div class="flex space-x-4">
                    <a href="#why" class="px-6 py-3 bg-white text-indigo-700 rounded-lg font-medium hover:bg-opacity-90 transition">探索指南</a>
                    <a href="#examples" class="px-6 py-3 border-2 border-white border-opacity-30 rounded-lg font-medium hover:bg-white hover:bg-opacity-10 transition">查看示例</a>
                </div>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <main class="container mx-auto max-w-5xl px-4 py-12">
        <!-- Why Section -->
        <section id="why" class="mb-20">
            <div class="mb-12">
                <h2 class="text-3xl font-bold mb-6 text-gray-800 flex items-center">
                    <span class="w-8 h-8 bg-indigo-100 text-indigo-700 rounded-full flex items-center justify-center mr-3">1</span>
                    为什么需要高效的流水线脚本？
                </h2>
                <p class="text-lg text-gray-600 mb-8">
                    在现代软件开发中，持续集成和持续交付（CI/CD）已经成为不可或缺的一部分。流水线脚本（Pipeline Script）是 CI/CD 的核心，它定义了从代码提交到部署的整个自动化流程。
                </p>
            </div>

            <div class="grid md:grid-cols-2 gap-8">
                <div class="bg-white p-8 rounded-xl shadow-md hover-grow">
                    <div class="text-indigo-600 text-3xl mb-4">
                        <i class="fas fa-bolt"></i>
                    </div>
                    <h3 class="text-xl font-bold mb-3">提高开发效率</h3>
                    <p class="text-gray-600">流水线脚本可以自动化构建、测试和部署流程，减少手动操作，提高开发效率。</p>
                </div>
                <div class="bg-white p-8 rounded-xl shadow-md hover-grow">
                    <div class="text-indigo-600 text-3xl mb-4">
                        <i class="fas fa-shield-alt"></i>
                    </div>
                    <h3 class="text-xl font-bold mb-3">减少人为错误</h3>
                    <p class="text-gray-600">自动化流程可以减少人为错误，如忘记运行测试、部署错误的版本等。</p>
                </div>
                <div class="bg-white p-8 rounded-xl shadow-md hover-grow">
                    <div class="text-indigo-600 text-3xl mb-4">
                        <i class="fas fa-search"></i>
                    </div>
                    <h3 class="text-xl font-bold mb-3">确保代码质量</h3>
                    <p class="text-gray-600">流水线脚本可以在每次代码提交时自动运行测试和代码检查，确保代码质量。</p>
                </div>
                <div class="bg-white p-8 rounded-xl shadow-md hover-grow">
                    <div class="text-indigo-600 text-3xl mb-4">
                        <i class="fas fa-rocket"></i>
                    </div>
                    <h3 class="text-xl font-bold mb-3">快速交付</h3>
                    <p class="text-gray-600">通过自动化部署流程，可以快速将新功能和修复交付给用户，缩短交付周期。</p>
                </div>
            </div>
        </section>

        <!-- Structure Section -->
        <section id="structure" class="mb-20">
            <h2 class="text-3xl font-bold mb-12 text-gray-800 flex items-center">
                <span class="w-8 h-8 bg-indigo-100 text-indigo-700 rounded-full flex items-center justify-center mr-3">2</span>
                流水线脚本的基本结构
            </h2>

            <div class="mb-8">
                <p class="text-lg text-gray-600 mb-8">
                    一个典型的流水线脚本通常包含以下几个阶段：
                </p>
            </div>

            <div class="grid md:grid-cols-2 lg:grid-cols-5 gap-6">
                <div class="stage-item relative bg-white p-6 rounded-lg shadow-md hover-grow">
                    <div class="text-indigo-600 text-2xl mb-3">
                        <i class="fas fa-code-branch"></i>
                    </div>
                    <h3 class="font-bold mb-2">代码拉取</h3>
                    <p class="text-sm text-gray-600">从版本控制系统（如 Git）拉取最新代码。</p>
                </div>
                <div class="stage-item relative bg-white p-6 rounded-lg shadow-md hover-grow">
                    <div class="text-indigo-600 text-2xl mb-3">
                        <i class="fas fa-hammer"></i>
                    </div>
                    <h3 class="font-bold mb-2">构建</h3>
                    <p class="text-sm text-gray-600">编译代码，生成可执行文件或包。</p>
                </div>
                <div class="stage-item relative bg-white p-6 rounded-lg shadow-md hover-grow">
                    <div class="text-indigo-600 text-2xl mb-3">
                        <i class="fas fa-vial"></i>
                    </div>
                    <h3 class="font-bold mb-2">测试</h3>
                    <p class="text-sm text-gray-600">运行单元测试、集成测试和端到端测试。</p>
                </div>
                <div class="stage-item relative bg-white p-6 rounded-lg shadow-md hover-grow">
                    <div class="text-indigo-600 text-2xl mb-3">
                        <i class="fas fa-search-plus"></i>
                    </div>
                    <h3 class="font-bold mb-2">代码检查</h3>
                    <p class="text-sm text-gray-600">运行静态代码分析工具，检查代码质量。</p>
                </div>
                <div class="stage-item relative bg-white p-6 rounded-lg shadow-md hover-grow">
                    <div class="text-indigo-600 text-2xl mb-3">
                        <i class="fas fa-server"></i>
                    </div>
                    <h3 class="font-bold mb-2">部署</h3>
                    <p class="text-sm text-gray-600">将构建结果部署到测试环境或生产环境。</p>
                </div>
            </div>
        </section>

        <!-- Efficient Scripts Section -->
        <section id="efficient" class="mb-20">
            <h2 class="text-3xl font-bold mb-12 text-gray-800 flex items-center">
                <span class="w-8 h-8 bg-indigo-100 text-indigo-700 rounded-full flex items-center justify-center mr-3">3</span>
                编写高效的流水线脚本
            </h2>

            <div class="space-y-12">
                <div class="grid md:grid-cols-2 gap-8 items-center">
                    <div>
                        <h3 class="text-2xl font-bold mb-4 flex items-center">
                            <span class="w-6 h-6 bg-indigo-100 text-indigo-700 rounded-full flex items-center justify-center mr-3 text-sm">3.1</span>
                            选择合适的工具
                        </h3>
                        <p class="text-gray-600 mb-4">
                            选择合适的 CI/CD 工具是编写高效流水线脚本的第一步。常见的 CI/CD 工具包括 Jenkins、GitLab CI、GitHub Actions 等。
                        </p>
                    </div>
                    <div class="bg-white rounded-xl shadow-md overflow-hidden">
                        <div class="p-5">
                            <div class="mb-4">
                                <div class="flex items-center mb-3">
                                    <div class="w-3 h-3 bg-red-500 rounded-full mr-2"></div>
                                    <h4 class="font-bold">Jenkins</h4>
                                </div>
                                <p class="text-sm text-gray-600 pl-5">功能强大，插件丰富，适合复杂项目。</p>
                            </div>
                            <div class="mb-4">
                                <div class="flex items-center mb-3">
                                    <div class="w-3 h-3 bg-orange-500 rounded-full mr-2"></div>
                                    <h4 class="font-bold">GitLab CI</h4>
                                </div>
                                <p class="text-sm text-gray-600 pl-5">与 GitLab 深度集成，配置简单，适合中小型项目。</p>
                            </div>
                            <div>
                                <div class="flex items-center mb-3">
                                    <div class="w-3 h-3 bg-blue-500 rounded-full mr-2"></div>
                                    <h4 class="font-bold">GitHub Actions</h4>
                                </div>
                                <p class="text-sm text-gray-600 pl-5">与 GitHub 深度集成，支持丰富的第三方插件，适合开源项目。</p>
                            </div>
                        </div>
                    </div>
                </div>

                <div>
                    <h3 class="text-2xl font-bold mb-4 flex items-center">
                        <span class="w-6 h-6 bg-indigo-100 text-indigo-700 rounded-full flex items-center justify-center mr-3 text-sm">3.2</span>
                        定义清晰的阶段
                    </h3>
                    <p class="text-gray-600 mb-6">
                        将流水线脚本划分为多个清晰的阶段，每个阶段有明确的职责。
                    </p>
                    <div class="code-block">
                        <div class="code-header">
                            <span class="text-sm text-gray-400">.gitlab-ci.yml</span>
                            <button class="text-gray-400 hover:text-white">
                                <i class="far fa-copy"></i>
                            </button>
                        </div>
                        <div class="code-content">
                            <pre class="text-sm"><code>stages:
  - build
  - test
  - code-quality
  - deploy</code></pre>
                        </div>
                    </div>
                </div>

                <div>
                    <h3 class="text-2xl font-bold mb-4 flex items-center">
                        <span class="w-6 h-6 bg-indigo-100 text-indigo-700 rounded-full flex items-center justify-center mr-3 text-sm">3.3</span>
                        并行化任务
                    </h3>
                    <p class="text-gray-600 mb-6">
                        在流水线脚本中，可以并行化独立的任务，以缩短整体执行时间。
                    </p>
                    <div class="code-block">
                        <div class="code-header">
                            <span class="text-sm text-gray-400">.gitlab-ci.yml</span>
                            <button class="text-gray-400 hover:text-white">
                                <i class="far fa-copy"></i>
                            </button>
                        </div>
                        <div class="code-content">
                            <pre class="text-sm"><code>jobs:
  build:
    stage: build
    script:
      - mvn clean package

  unit-test:
    stage: test
    script:
      - mvn test

  code-quality:
    stage: code-quality
    script:
      - mvn spotless:check</code></pre>
                        </div>
                    </div>
                </div>

                <div>
                    <h3 class="text-2xl font-bold mb-4 flex items-center">
                        <span class="w-6 h-6 bg-indigo-100 text-indigo-700 rounded-full flex items-center justify-center mr-3 text-sm">3.4</span>
                        使用缓存
                    </h3>
                    <p class="text-gray-600 mb-6">
                        使用缓存可以避免重复下载依赖和构建中间结果，从而加快流水线执行速度。
                    </p>
                    <div class="code-block">
                        <div class="code-header">
                            <span class="text-sm text-gray-400">.gitlab-ci.yml</span>
                            <button class="text-gray-400 hover:text-white">
                                <i class="far fa-copy"></i>
                            </button>
                        </div>
                        <div class="code-content">
                            <pre class="text-sm"><code>cache:
  paths:
    - .m2/repository/</code></pre>
                        </div>
                    </div>
                </div>

                <div>
                    <h3 class="text-2xl font-bold mb-4 flex items-center">
                        <span class="w-6 h-6 bg-indigo-100 text-indigo-700 rounded-full flex items-center justify-center mr-3 text-sm">3.5</span>
                        环境变量管理
                    </h3>
                    <p class="text-gray-600 mb-6">
                        使用环境变量管理敏感信息（如 API 密钥、数据库密码）和配置参数（如部署环境）。
                    </p>
                    <div class="code-block">
                        <div class="code-header">
                            <span class="text-sm text-gray-400">.gitlab-ci.yml</span>
                            <button class="text-gray-400 hover:text-white">
                                <i class="far fa-copy"></i>
                            </button>
                        </div>
                        <div class="code-content">
                            <pre class="text-sm"><code>variables:
  DATABASE_URL: "jdbc:mysql://localhost:3306/ts_db"
  DEPLOY_ENV: "production"</code></pre>
                        </div>
                    </div>
                </div>

                <div>
                    <h3 class="text-2xl font-bold mb-4 flex items-center">
                        <span class="w-6 h-6 bg-indigo-100 text-indigo-700 rounded-full flex items-center justify-center mr-3 text-sm">3.6</span>
                        错误处理和重试机制
                    </h3>
                    <p class="text-gray-600 mb-6">
                        在流水线脚本中，添加错误处理和重试机制，以提高流水线的稳定性。
                    </p>
                    <div class="code-block">
                        <div class="code-header">
                            <span class="text-sm text-gray-400">.gitlab-ci.yml</span>
                            <button class="text-gray-400 hover:text-white">
                                <i class="far fa-copy"></i>
                            </button>
                        </div>
                        <div class="code-content">
                            <pre class="text-sm"><code>deploy:
  stage: deploy
  script:
    - ./deploy.sh
  retry:
    max: 2
    when:
      - script_failure</code></pre>
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- Examples Section -->
        <section id="examples" class="mb-20">
            <h2 class="text-3xl font-bold mb-12 text-gray-800 flex items-center">
                <span class="w-8 h-8 bg-indigo-100 text-indigo-700 rounded-full flex items-center justify-center mr-3">4</span>
                实际项目中的应用
            </h2>

            <div class="space-y-16">
                <div>
                    <h3 class="text-2xl font-bold mb-6 flex items-center">
                        <span class="w-6 h-6 bg-indigo-100 text-indigo-700 rounded-full flex items-center justify-center mr-3 text-sm">4.1</span>
                        在 <code class="bg-indigo-100 text-indigo-700 px-2 py-1 rounded">ts-web</code> 项目中使用 Jenkins
                    </h3>
                    <div class="code-block">
                        <div class="code-header">
                            <span class="text-sm text-gray-400">Jenkinsfile</span>
                            <button class="text-gray-400 hover:text-white">
                                <i class="far fa-copy"></i>
                            </button>
                        </div>
                        <div class="code-content">
                            <pre class="text-sm"><code>pipeline {
    agent any

    stages {
        stage('Build') {
            steps {
                sh 'mvn clean package'
            }
        }

        stage('Test') {
            steps {
                sh 'mvn test'
            }
        }

        stage('Code Quality') {
            steps {
                sh 'mvn spotless:check'
            }
        }

        stage('Deploy') {
            steps {
                sh './deploy.sh'
            }
        }
    }

    post {
        success {
            echo 'Pipeline succeeded!'
        }
        failure {
            echo 'Pipeline failed!'
        }
    }
}</code></pre>
                        </div>
                    </div>
                </div>

                <div>
                    <h3 class="text-2xl font-bold mb-6 flex items-center">
                        <span class="w-6 h-6 bg-indigo-100 text-indigo-700 rounded-full flex items-center justify-center mr-3 text-sm">4.2</span>
                        在 <code class="bg-indigo-100 text-indigo-700 px-2 py-1 rounded">ts-api</code> 项目中使用 GitLab CI
                    </h3>
                    <div class="code-block">
                        <div class="code-header">
                            <span class="text-sm text-gray-400">.gitlab-ci.yml</span>
                            <button class="text-gray-400 hover:text-white">
                                <i class="far fa-copy"></i>
                            </button>
                        </div>
                        <div class="code-content">
                            <pre class="text-sm"><code>stages:
  - build
  - test
  - code-quality
  - deploy

build:
  stage: build
  script:
    - mvn clean package

unit-test:
  stage: test
  script:
    - mvn test

code-quality:
  stage: code-quality
  script:
    - mvn spotless:check

deploy:
  stage: deploy
  script:
    - ./deploy.sh
  only:
    - main</code></pre>
                        </div>
                    </div>
                </div>

                <div>
                    <h3 class="text-2xl font-bold mb-6 flex items-center">
                        <span class="w-6 h-6 bg-indigo-100 text-indigo-700 rounded-full flex items-center justify-center mr-3 text-sm">4.3</span>
                        在 <code class="bg-indigo-100 text-indigo-700 px-2 py-1 rounded">ts-mobile</code> 项目中使用 GitHub Actions
                    </h3>
                    <div class="code-block">
                        <div class="code-header">
                            <span class="text-sm text-gray-400">ci.yml</span>
                            <button class="text-gray-400 hover:text-white">
                                <i class="far fa-copy"></i>
                            </button>
                        </div>
                        <div class="code-content">
                            <pre class="text-sm"><code>name: CI Pipeline

on:
  push:
    branches:
      - main
  pull_request:
    branches:
      - main

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v2

      - name: Set up JDK 11
        uses: actions/setup-java@v2
        with:
          java-version: '11'
          distribution: 'adopt'

      - name: Build with Maven
        run: mvn clean package

      - name: Run unit tests
        run: mvn test

      - name: Run code quality check
        run: mvn spotless:check

      - name: Deploy
        run: ./deploy.sh</code></pre>
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- Best Practices Section -->
        <section id="best-practices" class="mb-20">
            <h2 class="text-3xl font-bold mb-12 text-gray-800 flex items-center">
                <span class="w-8 h-8 bg-indigo-100 text-indigo-700 rounded-full flex items-center justify-center mr-3">5</span>
                最佳实践
            </h2>

            <div class="grid md:grid-cols-2 gap-8">
                <div class="bg-white p-8 rounded-xl shadow-md hover-grow">
                    <div class="text-indigo-600 text-3xl mb-4">
                        <i class="fas fa-code"></i>
                    </div>
                    <h3 class="text-xl font-bold mb-3">保持脚本简洁</h3>
                    <p class="text-gray-600">流水线脚本应尽量简洁，避免复杂的逻辑。可以将复杂的任务封装在脚本文件中，然后在流水线脚本中调用。</p>
                </div>
                <div class="bg-white p-8 rounded-xl shadow-md hover-grow">
                    <div class="text-indigo-600 text-3xl mb-4">
                        <i class="fas fa-tachometer-alt"></i>
                    </div>
                    <h3 class="text-xl font-bold mb-3">定期优化</h3>
                    <p class="text-gray-600">定期优化流水线脚本，移除不必要的任务，并行化独立任务，使用缓存等，以提高执行效率。</p>
                </div>
                <div class="bg-white p-8 rounded-xl shadow-md hover-grow">
                    <div class="text-indigo-600 text-3xl mb-4">
                        <i class="fas fa-book"></i>
                    </div>
                    <h3 class="text-xl font-bold mb-3">文档化</h3>
                    <p class="text-gray-600">为流水线脚本编写文档，说明每个阶段的任务和配置参数，便于团队成员理解和使用。</p>
                </div>
                <div class="bg-white p-8 rounded-xl shadow-md hover-grow">
                    <div class="text-indigo-600 text-3xl mb-4">
                        <i class="fas fa-bell"></i>
                    </div>
                    <h3 class="text-xl font-bold mb-3">监控和报警</h3>
                    <p class="text-gray-600">设置监控和报警机制，及时发现和解决流水线脚本中的问题，确保流水线的稳定性。</p>
                </div>
            </div>
        </section>

        <!-- Visualization -->
        <section class="mb-20">
            <h2 class="text-3xl font-bold mb-8 text-gray-800">CI/CD流程可视化</h2>
            <div class="bg-white p-6 rounded-xl shadow-md">
                <div class="mermaid">
                    flowchart LR
                        A[代码提交] --> B[代码拉取]
                        B --> C[构建]
                        C --> D[测试]
                        D --> E[代码检查]
                        E --> F[部署]
                        style A fill:#6e8efb,color:#fff
                        style B fill:#a777e3,color:#fff
                        style C fill:#4fd1c5,color:#fff
                        style D fill:#f6ad55,color:#fff
                        style E fill:#f687b3,color:#fff
                        style F fill:#48bb78,color:#fff
                </div>
            </div>
        </section>
    </main>

    <!-- Footer -->
    <footer class="bg-gray-900 text-gray-300 py-12">
        <div class="container mx-auto max-w-5xl px-4">
            <div class="text-center">
                <div class="mb-4">
                    <h3 class="text-xl font-bold text-white mb-2">技术小馆</h3>
                    <p class="text-gray-400">探索技术之美，品味代码之韵</p>
                </div>
                <div>
                    <a href="http://www.yuque.com/jtostring" class="text-indigo-400 hover:text-indigo-300 transition">http://www.yuque.com/jtostring</a>
                </div>
            </div>
        </div>
    </footer>

    <script>
        mermaid.initialize({
            startOnLoad: true,
            theme: 'default',
            flowchart: {
                useMaxWidth: true,
                htmlLabels: true,
                curve: 'basis'
            }
        });

        // Copy code blocks
        document.querySelectorAll('.code-header button').forEach(button => {
            button.addEventListener('click', () => {
                const codeBlock = button.parentElement.nextElementSibling;
                const code = codeBlock.querySelector('code').innerText;
                navigator.clipboard.writeText(code).then(() => {
                    const originalIcon = button.innerHTML;
                    button.innerHTML = '<i class="fas fa-check"></i>';
                    setTimeout(() => {
                        button.innerHTML = originalIcon;
                    }, 2000);
                });
            });
        });
    </script>
</body>
</html>